package com.liaoct.lct.activity;

import java.util.ArrayList;
import java.util.List;

import com.liaoct.lct.fragment.CallsFragment;
import com.liaoct.lct.fragment.LinkManFragment;
import com.liaoct.lct.fragment.ShortMessageFragment;
import com.liaoct.lct.listener.TabMenuButtonClickListener;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 通讯录主Activity
 * 
 * @author liaoct
 * 
 */
public class MainActivity extends BaseActivity {

	private long mExitTime;

	/**
	 * 顶部标题栏内容数组
	 */
	public static String[] appHeaderTitles;
	/**
	 * 底部选项卡按钮组
	 */
	private LinearLayout tabMenus;

	/**
	 * 选项卡按钮组的按钮内容数组
	 */
	public static String[] tabMenuTitles;

	/**
	 * 选项卡按钮组
	 */
	private List<Button> tabMenuList;

	/**
	 * 片段管理器
	 */
	private FragmentManager fragmentManager = null;

	/**
	 * 片段事务
	 */
	private FragmentTransaction transcation = null;

	private void init() {
		appHeaderTitles = this.getResources()
				.getStringArray(R.array.appHeaders);
		appHeader = (TextView) this.findViewById(R.id.appHeader);
		appHeader.setText(appHeaderTitles[0]);
		tabMenuTitles = this.getResources().getStringArray(R.array.tabMenus);
		tabMenus = (LinearLayout) this.findViewById(R.id.tab_menus);
		tabMenuList = new ArrayList<Button>();
		for (int i = 0; i < tabMenuTitles.length; i++) {
			Button button = (Button) this.getLayoutInflater().inflate(
					R.layout.tab_button, null);
			button.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f));
			button.setText(tabMenuTitles[i]);
			button.setOnClickListener(new TabMenuButtonClickListener(i,
					MainActivity.this, tabMenuList));
			tabMenuList.add(button);
			tabMenus.addView(button);
		}
		fragmentManager = this.getFragmentManager();
		tabMenuList.get(0).performClick();
	}

	@Override
	public void performTranscation(int index) {
		Fragment fragment = null;
		switch (index) {
		case 0:// 联系人
			fragment = new LinkManFragment();
			break;
		case 1:// 通话记录
			fragment = new CallsFragment();
			break;
		case 2:// 短信息
			fragment = new ShortMessageFragment();
			break;
		}
		transcation = fragmentManager.beginTransaction();
		transcation.replace(R.id.tab_content, fragment);
		transcation.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
		transcation.commit();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean result = false;
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			long secondTime = System.currentTimeMillis();
			if (secondTime - mExitTime > 800) {// 如果两次按键时间间隔大于800毫秒，则不退出
				Toast.makeText(MainActivity.this, "再按一次退出程序...",
						Toast.LENGTH_SHORT).show();
				mExitTime = secondTime;// 更新firstTime
				result = true;
			} else {
				System.exit(0);// 否则退出程序
			}
		}
		return result;
	}
}
